#include<iostream>
#include<string>
using namespace std;
const int N = 5e3 + 10;
int f[N][N];
bool st[N][N];
int n, q;
string s;
int main()
{
	cin >> s;
	s = " " + s;
	n = s.size();
	for (int len = 1; len <= n; len++) {
		for (int l = 1; l + len - 1 <= n; l++) {
			int r = l + len - 1;
			if (s[l] != s[r])continue;
			if (l == r || l + 1 == r || st[l + 1][r - 1])st[l][r] = 1;
		}
	}
	for (int i = 1; i <= n; i++)f[i][i] = 1;
	for (int len = 1; len <= n; len++) {
		for (int l = 1; l + len - 1 <= n; l++) {
			int r = l + len - 1;
			f[l][r] = f[l][r - 1] + f[l + 1][r] - f[l + 1][r - 1] + st[l][r];
		}
	}
	cin >> q;
	while (q--) {
		int l, r;
		scanf("%d%d", &l, &r);
		printf("%d\n", f[l][r]);
	}
	return 0;
}